package cn.myframe.utils.ssh;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.Session;

//@Data
public class SSHData {
	
	
	private InputStream inputStream;
	private OutputStream outputStream;
	private Session session;
	private ChannelShell channel;
	private Long time;

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public OutputStream getOutputStream() {
		return outputStream;
	}

	public void setOutputStream(OutputStream outputStream) {
		this.outputStream = outputStream;
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public ChannelShell getChannel() {
		return channel;
	}

	public void setChannel(ChannelShell channel) {
		this.channel = channel;
	}

	public Long getTime() {
		return time;
	}

	public void setTime(Long time) {
		this.time = time;
	}

	public SSHData(){
		
	}
	
    public SSHData(Session session, ChannelShell channel){
		try {
			this.inputStream = channel.getInputStream();
			this.outputStream = channel.getOutputStream();
			this.session = session;
			this.channel = channel;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		this.time = new Date().getTime();
	}
    
    /**
     * 被调用时刷新时间
     */
    public void freshTime(){
    	this.time = new Date().getTime();
    }
    
    /**
     * 释放资源
     */
    public void destory(){
    	try{
    		if(this.inputStream != null){
        		this.inputStream.close();
        	}
    		if(this.outputStream != null){
        		this.outputStream.close();
        	}
    		if(this.channel != null){
        		this.channel.disconnect();
        	}
    		if(this.session != null){
        		this.session.disconnect();
        	}
    	}catch(Exception e){
    		
    	}
    }
    
    /**
     * 判断时间过期
     * @param curTime
     * @return
     */
    public boolean checkOutTime(long curTime){
    	if(new Date().getTime()-this.time > curTime){
    		return true;
    	}
    	return false;
    }

}
